package com.example.teatea.controller;

import com.example.teatea.dao.Update;
import com.example.teatea.project.Member;
import com.example.teatea.project.Staff;
import com.example.teatea.tools.GetTime;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.stage.Stage;

import javax.swing.*;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.ResourceBundle;

public class ChangeStaffController implements Initializable {
    public TextField sName;
    public TextField sPhone;
    public ComboBox sSex;
    public DatePicker sTime;
    public Button change;
    public Button returnBt;
    public boolean isedit = true;
    public TextField name;
    public TextField phone;
    public Button sureBt;
    public DatePicker time;
    public RadioButton man;
    public ToggleGroup sex;
    public RadioButton woman;
    public Button cancleBt;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        name.setText(MainController.s.getsName());
        phone.setText(MainController.s.getsPhone());
        String s = MainController.s.getsSex();
        if(s.equals("女")){
            woman.setSelected(true);
        }
        String date = MainController.s.getsTime();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate date1 = LocalDate.parse(date,fmt);
        time.setValue(date1);
    }

    public void toSure(ActionEvent actionEvent) {
        Staff staff = new Staff();
        if(name.getText().equals("")){
            JOptionPane.showMessageDialog(null, "姓名不能为空！", "提示", JOptionPane.ERROR_MESSAGE);
        }else{
            staff.setsName(name.getText());
            staff.setsPhone(phone.getText());
            staff.setsSex(sex.getSelectedToggle().getUserData().toString());
            staff.setsTime(time.getValue().toString());
            int r = new Update().staffUpdate(staff);
            if(r > 0){
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText("数据修改成功！");
                alert.show();
                Stage stage = (Stage) sureBt.getScene().getWindow();
                stage.close();
                MainController mainController = (MainController) ControllerManager.controllerMap.get("main");
                mainController.loadStaffDate();
            }
        }
    }

    public void toCancle(ActionEvent actionEvent) {
        Stage stage = (Stage) cancleBt.getScene().getWindow();
        stage.close();
    }
}
